﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using static LiWi.Shell;
namespace LiWi
{
    public partial class 位置比较输出与锁存 : Form
    {
        public WindowsAdapt adapt = new WindowsAdapt();


        IntPtr _handle { get { return Data.handle; } }
        bool isConnect { get { return Data.handle == (IntPtr)0 ? false : true; } }
        bool registIsRun=false;
        Thread loopThread = null;
        bool loopKey = false;
        public 位置比较输出与锁存()
        {
            InitializeComponent();
            this.DoubleBuffered = true;
            for (int i = 0; i < 1000; i++)
            {
                dataGridViewHw.Rows.Add();
                dataGridViewHw.Rows[i].Cells[0].Value = Convert.ToString(i);
                dataGridViewHw.Rows[i].Cells[1].Value = Convert.ToString(5*(i+1));
            }
            adapt.SetForm(this);

            comboBoxState.SelectedIndex = 0;
            comboBoxDir.SelectedIndex = 0;

            ComboBox_regist.SelectedIndex = 0;
            ComboBox_regist.DropDownStyle = ComboBoxStyle.DropDownList;

        }
        ~位置比较输出与锁存() { 
            loopKey = false;
            registIsRun = false;
        }
        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void label18_Click(object sender, EventArgs e)
        {

        }

        private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void Button2_Click(object sender, EventArgs e)
        {
            PMC_Rapidstop(_handle, 3);
            Task.Factory.StartNew(async () => {
                if (isConnect)
                {
                    Button2.Invoke(new Action(() => {
                        Button2.Enabled = false;
                        groupBox1.Enabled = false;
                    }));
                    PMC_Rapidstop(_handle, 3);
                    PMC_SetDpos(_handle, (int)numericUpDown_axis.Value, (float)numericUpDown_runStartPos.Value);
                    PMC_SetMpos(_handle, (int)numericUpDown_axis.Value, (float)numericUpDown_runStartPos.Value);
                    PMC_SetAtype(_handle, (int)numericUpDown_axis.Value, 1);
                    PMC_SetUnits(_handle, (int)numericUpDown_axis.Value, (float)numericUpDown_units.Value);
                    PMC_SetSpeed(_handle, (int)numericUpDown_axis.Value, (float)numericUpDown_speed.Value);
                    PMC_SetAccel(_handle, (int)numericUpDown_axis.Value, (float)numericUpDown_accel.Value);
                    PMC_SetDecel(_handle, (int)numericUpDown_axis.Value, (float)numericUpDown_decel.Value);

                    
                    if (checkBoxPswSelect.Checked)
                    {
                        int outNum = (int)numericUpDown_output.Value;
                        int startPointNum = (int)numericUpDownstartPoint.Value;
                        int endPointNum = (int)numericUpDown_endPoint.Value;
                        int startOutState = (int)comboBoxState.SelectedIndex;
                        int dir = (int)comboBoxDir.SelectedIndex;


                        if (startPointNum <= endPointNum && endPointNum < 1000)
                        {
                          //  float[] values = new float[endPointNum - startPointNum];
                            float[] f = new float[1];
                            for (int i = 0; i < endPointNum - startPointNum; i++)
                            {
                                // values[i] =  (float)Convert.ToDecimal( dataGridViewHw.Rows[startPointNum+i].Cells[1].Value);
                                f[0]= (float)Convert.ToDecimal(dataGridViewHw.Rows[startPointNum + i].Cells[1].Value);
                                PMC_SetTable(_handle, i, 1, f);
                            }


                           
                        }

                       PMC_HwPswitch2(_handle, (int)numericUpDown_axis.Value, 2, outNum, startOutState, 0, 0, 0, 0);
                       PMC_HwPswitch2(_handle, (int)numericUpDown_axis.Value, 1, outNum, startOutState, 0, (endPointNum - startPointNum)-1, dir, 0);

                      
                        if (checkBox_timer.Checked)
                        {   //定时反转
                            if (outNum >= 0)
                            {
                                PMC_HwTimer(_handle, 0, 0, 0, 0, 0, 0);  //先关闭
                                PMC_HwTimer(_handle, 2, (int)numericUpDownTimerCyc.Value, (int)numericUpDowntimerTime.Value, (int)numericUpDownTimerNumber.Value, startOutState, outNum);
                            }
                        }
                       
                    }
                    Thread.Sleep(200);

                    //判断缓冲剩余量:如果缓冲小于20等待一下
                    await Task.Run(() =>
                    {
                        int[] axisList = { (int)numericUpDown_axis.Value };
                        while (Data.ReMainBuffer(axisList) <= 20)
                        {
                            Thread.Sleep(200);
                        }

                        PMC_Single_MoveAbs(_handle, (int)numericUpDown_axis.Value, (float)numericUpDown_runEndPos.Value);
                        int idle = 0;
                        while (idle == 0)
                        {
                            PMC_GetIfIdle(_handle, (int)numericUpDown_axis.Value, ref idle);
                            Thread.Sleep(1);
                        }
                        Button2.Invoke(new Action(() =>
                        {
                            Button2.Enabled = true;
                            groupBox1.Enabled = true;
                            PMC_HwTimer(_handle, 0, 0, 0, 0, 0, 0);  //先关闭
                        }));
                    });
                }
            });
        }

        private void Button3_Click(object sender, EventArgs e)
        {
            if(isConnect)
                PMC_Rapidstop(_handle, 3);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (isConnect) {

                uint opValue = 0;
                PMC_GetOp(_handle, (int)numericUpDown_output.Value, ref opValue);
                checkBox_opState.Checked = Convert.ToBoolean(opValue);

                float pos=0;
                PMC_GetDpos(_handle, (int)numericUpDown_axis.Value, ref pos);
                labelPos.Text = pos.ToString();

            }
        }

        private void button_regist_Click(object sender, EventArgs e)
        {
            if (isConnect)
            {
                if (!registIsRun)
                {
                    DataGridView_registTable.Rows.Clear();
                    button_regist.Text = "停止";
                    
                   
                    registIsRun = true;

                    Task.Factory.StartNew(() => {
                        int mode = 3;
                        if (ComboBox_regist.SelectedIndex == 0)
                            mode = 3;
                        else if(ComboBox_regist.SelectedIndex == 1)
                            mode = 4;
                        else if (ComboBox_regist.SelectedIndex == 2)
                            mode = 13;
                        else if (ComboBox_regist.SelectedIndex == 3)
                            mode = 14;
                        while (registIsRun) {
                            PMC_Regist(_handle, (int)numericUpDown_registAxis.Value, mode);
                            int mark = 0;
                            float pos = 0;
                            if (mode == 3 || mode == 4)
                            {
                                while (mark == 0 && registIsRun == true)
                                {
                                    PMC_GetMark(_handle, (int)numericUpDown_registAxis.Value, ref mark);
                                    if (!registIsRun)
                                        return;
                                }
                                PMC_GetRegPos(_handle, (int)numericUpDown_registAxis.Value, ref pos);

                            }
                            else {    //markb 
                                while (mark == 0 && registIsRun == true)
                                {
                                    PMC_GetMarkB(_handle, (int)numericUpDown_registAxis.Value, ref mark);
                                    if (!registIsRun)
                                        return;
                                }
                                PMC_GetRegPosB(_handle, (int)numericUpDown_registAxis.Value, ref pos);
                            }
                           
                            DataGridView_registTable.Invoke(new Action(() => {
                                DataGridView_registTable.Rows.Add(new string[] { Convert.ToString(DataGridView_registTable.Rows.Count) , Convert.ToString(pos) });
                            }));
                        }
                    });
                }
                else
                {
                    registIsRun = false;
                    button_regist.Text = "启动";

                }
            }
            else {
                button_regist.Text = "启动";
            }
        }


    }
}
